﻿using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Actions;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;

namespace CardPlay.DemoPack.Games
{
    [Game("5633306A-16A9-4F7D-A6D9-9A9E1FF83A0B", LevelOfDifficulty.Easy, LevelOfSkill.MostlySkill, DurationOfPlay.Within5To10Minutes, Family = FamilyName)]
    public class FlowerGarden : StandardCardGame
    {
        public const string FamilyName = "Flower Garden";

        public FlowerGarden()
        {
            NamesOfAdditionalPiles.Add(Reserve);

            TotalFoundations = 4;
            TotalTableaus = 6;
            TableauBuildSequence = CardFaceSequence.AnySuitDescendingRank;
        }

        public override ILayoutSite CreatePresentation(IPresentationContext presentation)
        {
            var foundations = presentation.CreateRowLayout();
            foundations.HorizontalContentAlignment = HorizontalAlignment.Center;

            var reserve = presentation.CreateRowLayout();
            reserve.HorizontalContentAlignment = HorizontalAlignment.Center;
            reserve.Add(presentation.CreatePileVisual(Reserve, new VisualTraits { Arrangement = CardArrangement.StackLeftToRight, ContentFit = ContentFit.FitBounds }));

            var tableau = presentation.CreateRowLayout();

            presentation.AddFoundationPiles(this, foundations, Rank.Ace);
            presentation.AddTableauPiles(this, tableau);

            var root = presentation.CreateColumnLayout();
            root.HorizontalContentAlignment = HorizontalAlignment.Center;

            root.Add(foundations);
            root.Add(reserve);
            root.Add(tableau);

            return root;
        }

        public override RemovePileAction CreatePickupActionAt(PartLocation location)
        {
            return (location.Matches(Reserve) && CardAt(location) != null) 
                ? new RemovePileAction(this, location, 1) 
                : base.CreatePickupActionAt(location);
        }

        protected override void PrepareDealScript(DealScript script)
        {
            script.Deal("16 up").To(Reserve);
            script.DealToEach(NamesOfTableaus, "6 up");
        }

        protected override void OverridePlayingRules(PlayingRules playingRules)
        {
            playingRules.At(Reserve).Permit<RemovePileAction>().When(Action.IsActingOnFaceUpCard);
        }

        protected override bool AllowedToPickupAtTableau(RemovePileAction action)
        {
            return action.IsActingOnFaceUpTopCard();
        }
    }
}